<?php
/**
 * Description: 申论 - 字迹诊断作业模块
 * User: devforma
 * Date: 16/5/26
 * Time: 12:05
 */

require __DIR__ . '/ProcessExerciseBase.class.php';

class ZijizhenduanModule extends ProcessExerciseBase {

    /**
     * 设置作业类型
     */
    protected function setExerciseType() {
        $this->data['exercise_type'] = 3;
    }

    /**
     * 设置作业题目
     */
    protected function setExerciseQuestion() {
        global $cmsdb;

        //获取题目和材料
        $questionInfo = $cmsdb->getRow('SELECT `id`, `content`, `right_answer` FROM `original_primary_question` WHERE `id`=:id', array(':id' => $this->questionIds[0]));

        $this->data['question'] = array(
            'question_id' => (int)$questionInfo->id,
            'content'     => $questionInfo->content,
            'image_url'   => $questionInfo->right_answer
        );
    }

    /**
     * 设置提交权限
     */
    protected function setSubmitPriv() {
        parent::setSubmitPriv();
    }

    /**
     * 设置用户答案
     */
    protected function setUserAnswer() {
        global $uddb;

        //查询用户做题记录
        $sql = 'SELECT * FROM `history_primary_question_review` WHERE `user_id`=:uid AND `exercise_id`=:eid LIMIT 1';
        $questionRecord = $uddb->getRow($sql, array(':uid' => $this->userId, ':eid' => $this->exerciseId));

        if (!empty($questionRecord)) { //有用户答题记录
            $this->data['user_answer'] = array(
                'submit_time'   => $questionRecord->action_time,
                'image_url'     => $questionRecord->user_answer,
                'review_postil' => $questionRecord->review_postil,
                'score'         => (float)$questionRecord->review_score
            );
        }
    }

    /**
     * 检测提交的参数
     * @param array $args 参数
     * @return bool
     */
    public function validateArgs($args) {
        if (!empty($args['image_url']) && !empty($args['question_id'])) {
            $this->submitArgs = $args;
            return true;
        }

        return false;
    }

    /**
     * 提交 - 设置作业状态
     */
    protected function setSubmitStatus() {
        if ($this->exerciseInfo->end_time > $this->now)
            $this->submitExerciseRecord['status'] = 3; //已提交
        else
            $this->submitExerciseRecord['status'] = 5; //过期完成
    }

    /**
     * 提交 - 保存答题记录及设置作业统计信息
     */
    protected function saveQuestionRecordsAndSetSubmitSummary() {
        global $uddb;

        //构造题目记录
        $questionRecord = array(
            'question_id'   => $this->submitArgs['question_id'],
            'exercise_id'   => $this->exerciseId,
            'user_id'       => $this->userId,
            'action_time'   => $this->now,
            'user_answer'   => $this->submitArgs['image_url'],
            'reviewer_type' => 0,
            'status'        => 1
        );

        //查询是否存在提交记录
        $sql = 'SELECT `id` FROM `history_primary_question_review` WHERE `user_id`=:uid AND `exercise_id`=:eid LIMIT 1';
        $exists = $uddb->getVar($sql, array(':uid' => $this->userId, ':eid' => $this->exerciseId));

        if ($exists) {
            $uddb->update('history_primary_question_review', $questionRecord, array('id' => $exists));
        } else {
            $uddb->insert('history_primary_question_review', $questionRecord);
        }

        $this->submitExerciseRecord['summary'] = '';
    }
    
}